//
//  CampusPreviewVideoListViewController.m
//  ShowYork
//
//  Created by hxh on 2021/3/26.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusPreviewVideoListViewController.h"
#import "CampusVlogDetailViewController.h"
#import "CampusPreviewVideoItemCell.h"
#import "CampusSchoolVideoItemModel.h"
@interface CampusPreviewVideoListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,assign)NSInteger pageIndex;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL lastPage;
@end

@implementation CampusPreviewVideoListViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = NO;

}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUI];
}
- (void)setUI{
    
    self.navigationItem.title = @"校园Vlog";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    layout.sectionInset = UIEdgeInsetsMake(16,16, 0,16);
    layout.itemSize = CGSizeMake((kScreenW - 32 - 8)/2,(kScreenW - 32 - 8)/2*16/9);
    layout.minimumLineSpacing = 8.0;
    layout.minimumInteritemSpacing = 4.0f;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[CampusPreviewVideoItemCell class] forCellWithReuseIdentifier:@"CampusPreviewVideoItemCell"];
    
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.pageIndex = 1;
    __weak typeof(self)weakSelf = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageIndex = 1;
        [weakSelf loadDataSource];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageIndex++;
        [weakSelf loadDataSource];
    }];

    
    [footer setTitle:@"—— 没有更多了 ——" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = font(10);
    footer.stateLabel.textColor = RGBA(153, 153, 153, 1);
    footer.hidden = YES;
    self.collectionView.mj_header = header;
    self.collectionView.mj_footer = footer;
    
    [self showDataLoadState];
    [self loadDataSource];
    
}

- (void)loadDataSource{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:SchoolVlogList];
    param[@"pageIndex"] = @(self.pageIndex);
    param[@"pageSize"] = @(10);
    param[@"id"] = self.schId;
    param[@"userId"] = [RPAppManager getCustomerId];
    [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(SchoolVlogList) parames:param success:^(id responseObj) {
        [self stopRefresh];
        [self hideDataLoadLoadState];
        if (self.pageIndex == 1) {
            self.dataArray = [CampusSchoolVideoItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]];
        }else{
            [self.dataArray addObjectsFromArray:[CampusSchoolVideoItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]]];
        }
        CGFloat bottom = self.collectionView.mj_insetB;
        if ([responseObj[RESPONSE][LASTPAGE]boolValue]) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            self.lastPage = YES;
            if (self.collectionView.mj_footer.cl_height == 44.0f) {
                self.collectionView.mj_footer.cl_height = 138.0f;
                if (bottom == 44.0f || bottom == GK_SAVEAREA_BTM + 44.0f) {
                    self.collectionView.mj_insetB = 138.0f;
                }
            }
        }else{
            [self.collectionView.mj_footer resetNoMoreData];
            self.lastPage = NO;
            if (self.collectionView.mj_footer.cl_height == 44.0f) {
                self.collectionView.mj_footer.cl_height = 138.0f;
                if (bottom == 138.0f) {
                     self.collectionView.mj_insetB = 44.0f;
                 }
            }
        }
        self.collectionView.mj_footer.hidden = self.dataArray.count == 0;
        
        [self.collectionView reloadData];
        
    } failure:^(id error) {
        [self stopRefresh];
        [self hideDataLoadLoadState];
    }];
}

- (void)stopRefresh{
    if ([self.collectionView.mj_header isRefreshing]) {
        [self.collectionView.mj_header endRefreshing];
    }
    if ([self.collectionView.mj_footer isRefreshing]) {
        [self.collectionView.mj_footer endRefreshing];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CampusPreviewVideoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CampusPreviewVideoItemCell" forIndexPath:indexPath];
    [cell setModel:self.dataArray[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CampusSchoolVideoItemModel *itemModel = self.dataArray[indexPath.item];

    [MobClick event:@"show_vlog_detail"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentIndex"] = @(indexPath.item);
    param[@"currentPage"] = @(self.pageIndex);
    param[@"listSource"] = self.dataArray;
    param[@"lastPage"] = @(self.lastPage);
    param[@"schoolId"] = itemModel.schoolId;
    param[@"schoolName"] = self.schoolName;
    CampusVlogDetailViewController *vc = [[CampusVlogDetailViewController alloc]initWithConfigInfo:param];
    [self.navigationController pushViewController:vc animated:YES];
}


- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


@end
